<?php
class Admin_Model_DAO_Article
{    
    private $_db;
    private static $_instance = null;
    
    /**
     * @return Admin_Model_DAO_Article
     * */
    public static function factory() {
    	if(self::$_instance == null) {
    		self::$_instance = new self();
    	}
    
    	return self::$_instance;
    }
    
    private function __construct()
    {
    	$module = Utils_Global::get('module');
    	$this->_db = Utils_Global::getDbInstance($module);
    }
    
    /**
     * Insert article
     * @param array $data
     * @return integer last insert id on success or 0 on failure
     * */
   public function insert($data) {
       $this->_db->insert('article', $data);
       $result = $this->_db->lastInsertId('article', 'id');
       return $result;
   }
   
   /**
    * Update article by given id and data
    * @param integer $id
    * @param array $data
    * @return true on success, false on failure
    * */
   public function update($id, $data) {
       return $this->_db->update('article', $data, "id = {$this->_db->quote($id, 'INTEGER')}");
   }
   
   /**
    * Delete article by given id
    * @param integer $id
    * @return true on success, false on failure
    * */
   public function delete($id) {
       return $this->_db->delete('article', "id = {$this->_db->quote($id, 'INTEGER')}");
   }
   
   /**
     * Get list of articles by given options
     * @param array $options
     * @return array
     * */
	public function getArticles($options = array()) {
		$sql = "SELECT `article`.*, `category`.`name` `category_name`,
		                `t`.`lang` `title_lang`, `t`.`content` `title_content`, `t`.`id` `title_id`, `t`.`seo_title` `title_seo`, 
		                `t2`.`lang` `short_description_lang`, `t2`.`content` `short_description_content`, `t2`.`id` `short_description_id`, `t2`.`seo_title` `short_description_seo`,
		                `t3`.`lang` `content_lang`, `t3`.`content` `content_content`, `t3`.`id` `content_id`, `t3`.`seo_title` `content_seo`,
		                `t4`.`seo_title` `seo_title_seo`, `t4`.`content` `seo_title_content`, `t4`.`lang` `seo_title_lang`, `t4`.`id` `seo_title_tran_id`,
		                `t5`.`seo_title` `seo_description_seo`, `t5`.`content` `seo_description_content`, `t5`.`lang` `seo_description_lang`, `t5`.`id` `seo_description_tran_id`,
		                `t6`.`seo_title` `seo_keyword_seo`, `t6`.`content` `seo_keyword_content`, `t6`.`lang` `seo_keyword_lang`, `t6`.`id` `seo_keyword_tran_id` ";
		$from = " FROM `article` LEFT JOIN `category` ON `article`.`category` = `category`.`id` ";
		$from2 = " INNER JOIN `translation` t ON `article`.`i18n_title` = `t`.`i18n_id` ";
		$from3 = " INNER JOIN `translation` t2 ON `article`.`i18n_short_description` = `t2`.`i18n_id` ";
		$from4 = " INNER JOIN `translation` t3 ON `article`.`i18n_content` = `t3`.`i18n_id` ";
		$from5 = " INNER JOIN `translation` t4 ON `article`.`i18n_seo_title` = `t4`.`i18n_id` ";
		$from6 = " INNER JOIN `translation` t5 ON `article`.`i18n_seo_description` = `t5`.`i18n_id` ";
		$from7 = " INNER JOIN `translation` t6 ON `article`.`i18n_seo_keywords` = `t6`.`i18n_id` ";
		$where = " WHERE 1 = 1 AND ((`t`.`lang` = 'en' AND `t2`.`lang` = 'en' AND `t3`.`lang` = 'en' AND `t4`.`lang` = 'en' AND `t5`.`lang` = 'en' AND `t6`.`lang` = 'en') OR 
		                        (`t`.`lang` = 'vi' AND `t2`.`lang` = 'vi' AND `t3`.`lang` = 'vi' AND `t4`.`lang` = 'vi' AND `t5`.`lang` = 'vi' AND `t6`.`lang` = 'vi')) ";
		if($options['editor']) {
		    $where .= " AND `editor` LIKE '%{$options['editor']}%' ";
		}
		if($options['top']) {
			$where .= " AND `top` = {$this->_db->quote($options['top'], 'INTEGER')} ";
		}
		if($options['category']) {
			$where .= " AND `category` = {$this->_db->quote($options['category'], 'INTEGER')} ";	
		}
		if($options['id']) {
			$where .= " AND `article`.`id` = {$this->_db->quote($options['id'], 'INTEGER')} ";
		}
		if($options['title']) {
		    $where .= " AND `t`.`content` LIKE '%" . $options['title'] . "%'";
		}
		if(isset($options['active']) && $options['active']) {
		    if($options['active'] == 2) {
		        $active = 0;
		    } else {
		        $active = 1;
		    }
		    $where .= " AND `article`.`active` = {$this->_db->quote($active, 'INTEGER')} ";
		}
		if($options['datelineF'] && $options['datelineT']) {
		    $where .= " AND `article`.`dateline` >= {$this->_db->quote($options['datelineF'], 'INTEGER')} 
		                AND `article`.`dateline` <= {$this->_db->quote($options['datelineT'], 'INTEGER')} ";
		}
		
		$order = " ORDER BY `article`.`id` DESC ";
		if($options['order'] && $options['by']) {
			$order = " ORDER BY `article`.`{$options['order']}` {$options['by']} ";
		}
		//$groupBy = " GROUP BY `t`.`id`, `t2`.`id`, `t3`.`id` ";
		$limit = "";
		if(isset($options['offset']) && $options['offset'] >=0 && $options['limit'] >0) {
			$limit = " limit {$this->_db->quote($options['offset'], 'INTEGER')}, {$this->_db->quote($options['limit'], 'INTEGER')} "; 
		}
// 		prBinh($sql . $from . $from2 . $from3 . $from4 . $from5 . $from6 . $from7 . $where . $order . $limit );
		$result = $this->_db->fetchAll ( $sql . $from . $from2 . $from3 . $from4 . $from5 . $from6 . $from7 . $where . $order . $limit );
		return $result;
	}
	
	/**
	 * Get article count by given options
	 * @param array $options
	 * @return integer
	 * */
	public function getArticleCount($options = array()) {
		$sql = "SELECT count(*) as `count` ";
		$from = " FROM `article` INNER JOIN `translation` t ON `article`.`i18n_title` = `t`.`i18n_id` ";
		$where = " WHERE 1 = 1 ";
		if($options['editor']) {
			$where .= " AND `editor` LIKE '%{$options['editor']}%' ";
		}
		if($options['top']) {
			$where .= " AND `top` = {$this->_db->quote($options['top'], 'INTEGER')} ";
		}
		if($options['category']) {
			$where .= " AND `category` = {$this->_db->quote($options['category'], 'INTEGER')} ";
		}
		if($options['id']) {
		    $where .= " AND `article`.`id` = {$this->_db->quote($options['id'], 'INTEGER')} ";
		}
		if($options['title']) {
			$where .= " AND `t`.`content` LIKE '%" . $options['title'] . "%'";
		}
	    if(isset($options['active']) && $options['active']) {
		    if($options['active'] == 2) {
		        $active = 0;
		    } else {
		        $active = 1;
		    }
		    $where .= " AND `article`.`active` = {$this->_db->quote($active, 'INTEGER')} ";
		}
		if($options['datelineF'] && $options['datelineT']) {
		    $where .= " AND `article`.`dateline` >= {$this->_db->quote($options['datelineF'], 'INTEGER')}
				AND `article`.`dateline` <= {$this->_db->quote($options['datelineT'], 'INTEGER')} ";
		}
		
		$result = $this->_db->fetchOne($sql . $from . $where);
		return $result;
	}
}